package com.tanhua.dubbo.server.api;

import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.LingHunCun;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service(version = "1.0.0")
public class LingHunApiImpl implements LingHunApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public boolean saveLingHunCun(LingHunCun lingHunCun) {

        //存到mongoDB
        this.mongoTemplate.save(lingHunCun);

        //校验
        if (lingHunCun.getUserid()==null){
            return false;
        }

        return true;
    }

    @Override
    public List lingHunList(Long userid) {
        //查询所有主键id
//        List all = this.mongoTemplate.findAll(LingHunCun.class);
//        return all;


        //排除id/nin
        Query query = Query.query(Criteria.where("userid").nin(userid));
        List<LingHunCun> lingHunCuns = this.mongoTemplate.find(query, LingHunCun.class);
        return lingHunCuns;
    }
}